# Programm sortiert Bilder nach Erstellungsdatum und legt diese
# in erstellte Ordner folgender Struktur Jahr -> Monat -> Tag

###############################################################
import os
import time
import sys
import shutil
import exifread
###############################################################

def Num2Month(inVar):
    if inVar == "01":
        return "Jan"
    elif inVar == "02":
        return "Feb"
    elif inVar == "03":
        return "Mar"
    elif inVar == "04":
        return "Apr"
    elif inVar == "05":
        return "May"
    elif inVar == "06":
        return "Jun"
    elif inVar == "07":
        return "Jul"
    elif inVar == "08":
        return "Aug"
    elif inVar == "09":
        return "Sep"
    elif inVar == "10":
        return "Oct"
    elif inVar == "11":
        return "Nov"
    else:
        return "Dec"
    
def CheckFileType(scrFile, fileType = "Image"): 
    videoFileExtensions = ["avi", "mpg", "mpeg", "3gp", "mov", "mkv"]
    imgFileExtensions = ["jpg", "jpeg"]
    retVar = 0
    
    if fileType == "Image":
        extensions = imgFileExtensions
        fileType = 1;
    elif fileType == "Video":
        extensions = videoFileExtensions
        fileType = 2;
    
    # fileName  = scrFile.split("\\")[-1]
    fileNameExt = scrFile.split(".")[-1].lower()
    
    if fileNameExt in extensions:
            retVar = fileType;
    
    return retVar

def DeleteEmptyFolder(path):
    
    fileList = os.listdir(path)
    
    # ist im Ordner was drin?
    if len(fileList) == 0:
        # wenn nein --> Ordner loeschen
        os.rmdir(path)
        
        # nur eine Datei "Thumbs.db"
    elif len(fileList) == 1 and fileList[0] == "Thumbs.db":
        os.remove(path + "\\Thumbs.db")
        os.rmdir(path)
        
    else:
        # sonst gehe liste durch und durchsuche jeden Ordner
        
        for name in fileList:
            nextPath = path + "\\" + name
    
            # Wenn Ordner
            if os.path.isdir(nextPath):
                # dann durchsuchen
                DeleteEmptyFolder(nextPath)
        
        # Liste bearbeitet -> versuch nochmal den Ordner zu loeschen
        fileList = os.listdir(path)
        if len(fileList) == 0:
            # wenn nein --> Ordner loeschen
            os.rmdir(path)
    return



def RenameFilesInFolders(path): 
    fileList = os.listdir(path)
    index = 1;
    
    for name in fileList:
        srcFile = path + "\\" + name
    
        if os.path.isdir(srcFile):
            RenameFilesInFolders(srcFile)
            
        else:
            # sonst die Datei umbenennen
            fileType = CheckFileType(srcFile, "Video")
            
            if fileType != 2:
                continue
            
            srcFileSplit = srcFile.split("\\");
        
            
            fileName  = srcFileSplit[-1];
            fileDay   = srcFileSplit[-2];
            fileMonth = srcFileSplit[-3];
            fileYear  = srcFileSplit[-4];
            
            fileExtension = fileName.split(".")[-1];
            
            if len(fileDay) == 1:
                fileDay = "0" + fileDay
            
            if len(fileList) > 1:
                filePartName = fileDay + "_" + fileMonth + "_" + fileYear + "_" + str(index) + "." + fileExtension
            else:
                filePartName = fileDay + "_" + fileMonth + "_" + fileYear + "." + fileExtension
                
             
            dstFile = path + "\\" + filePartName
            index = index + 1
            
            print(filePartName)
            
            shutil.move(srcFile, dstFile)
    

def main():
    
    if len(sys.argv) == 1:
        srcPath = os.getcwd()
    else:
        srcPath = sys.argv[1]
    
    # srcPath = srcPath + "/Quelle"
    
    # srcPath = "D:/FUH-Repo/trunk/EclipseWorkspaces/Python/workspace/Test_date_in_Name"
    
    #path = "F:\Alle Photos"
    #srcPath = "F:\Alle Photos\Quelle"
    
    # Aktuellen pfad rekursiv nach Dateien durchsuchen und absolute pfade zur Dateien in Liste merken
    RenameFilesInFolders(srcPath)

    
    
    time.sleep(3)


if __name__ == "__main__":
    main()
    